package com.ma4567.remotecontroller.MysqlHelper;

import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.UserManager.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class TableUserManager {
    public static void Init() throws SQLException {
        Connection conn = ConnectPool.getConnection();
        PreparedStatement stmt = conn.prepareStatement(
                "create table if not exists user" +
                "(   name          text       not null," +
                "    Account       int        not null primary key," +
                "    Password      text       not null," +
                "    Root          tinyint(1) not null," +
                "    DownloadFile  tinyint(1) not null," +
                "    DeleteFile    tinyint(1) not null," +
                "    UploadFile    tinyint(1) not null," +
                "    RemoteControl tinyint(1) not null)" +
                        "    character set utf8 engine InnoDB;");
        stmt.executeUpdate();
        conn.close();

        boolean RootExists = false;
        for (User u: GetUserList()){
            if(u.Root){
                RootExists = true;
            }
        }
        if(!RootExists){
            Connection conn1 = ConnectPool.getConnection();

            User newUser = new User("root");

            PreparedStatement stmt1 = conn1.prepareStatement("insert into user(name,Account,Password,Root,DownloadFile,DeleteFile,UploadFile,RemoteControl) " +
                    "values (?,?,?,?,?,?,?,?)");
            stmt1.setString(1, newUser.name);
            stmt1.setInt(2, newUser.Account);
            stmt1.setString(3, newUser.Password);
            stmt1.setBoolean(4, newUser.Root);
            stmt1.setBoolean(5, newUser.DownloadFile);
            stmt1.setBoolean(6, newUser.DeleteFile);
            stmt1.setBoolean(7, newUser.UploadFile);
            stmt1.setBoolean(8, newUser.RemoteControl);
            stmt1.executeUpdate();
            RemoteControllerApplication.userManager.users.add(newUser);

            conn1.close();
        }
    }

    public static ArrayList<User> GetUserList() throws SQLException {
        Connection conn = ConnectPool.getConnection();
        ArrayList<User> result = new ArrayList<>();

        PreparedStatement stmt = conn.prepareStatement("select * from user;");

        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            User temp = new User(rs.getString("name"), rs.getInt("Account"), rs.getString("Password"), rs.getBoolean("Root"), rs.getBoolean("DownloadFile"), rs.getBoolean("DeleteFile"), rs.getBoolean("UploadFile"), rs.getBoolean("RemoteControl"));
            result.add(temp);
        }

        conn.close();
        return result;
    }

    public static User GetUser(int Account) throws SQLException {
        Connection conn = ConnectPool.getConnection();
        User result = null;

        PreparedStatement stmt = conn.prepareStatement("select * from user where Account = ?;");
        stmt.setInt(1, Account);

        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            result = new User(rs.getString("name"), rs.getInt("Account"), rs.getString("Password"), rs.getBoolean("Root"), rs.getBoolean("DownloadFile"), rs.getBoolean("DeleteFile"), rs.getBoolean("UploadFile"), rs.getBoolean("RemoteControl"));
        }

        conn.close();
        return result;
    }

    public static User GetUser(String name) throws SQLException {
        Connection conn = ConnectPool.getConnection();
        User result = null;

        PreparedStatement stmt = conn.prepareStatement("select * from user where name = ?;");
        stmt.setString(1, name);

        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            result = new User(rs.getString("name"), rs.getInt("Account"), rs.getString("Password"), rs.getBoolean("Root"), rs.getBoolean("DownloadFile"), rs.getBoolean("DeleteFile"), rs.getBoolean("UploadFile"), rs.getBoolean("RemoteControl"));
        }

        conn.close();
        return result;
    }

    public static void UpdataUserName(int Account, String name) throws SQLException {
        Connection conn = ConnectPool.getConnection();

        PreparedStatement stmt = conn.prepareStatement("update user set name = ? where Account = ?;");
        stmt.setString(1, name);
        stmt.setInt(2, Account);

        stmt.executeUpdate();

        conn.close();
    }

    public static void UpdataUserPassword(int Account, String Password) throws SQLException {
        Connection conn = ConnectPool.getConnection();

        PreparedStatement stmt = conn.prepareStatement("update user set Password = ? where Account = ?;");
        stmt.setString(1, Password);
        stmt.setInt(2, Account);

        stmt.executeUpdate();

        conn.close();
    }

    public static void UpdateUserPermission(int Account, String PermissionName, boolean permission) throws SQLException {
        Connection conn = ConnectPool.getConnection();

        PreparedStatement stmt = conn.prepareStatement("update user set Password = ? where Account = ?;");

        switch(PermissionName){
            case "DownloadFile":
                stmt = conn.prepareStatement("update user set DownloadFile = ?, UploadFile = ?, DeleteFile = ? where Account = ?;");
                stmt.setBoolean(1, permission);
                stmt.setBoolean(2, permission);
                stmt.setBoolean(3, permission);
                stmt.setInt(4, Account);
                break;
            case "RemoteControl" :
                stmt = conn.prepareStatement("update user set RemoteControl = ? where Account = ?;");
                stmt.setBoolean(1, permission);
                stmt.setInt(2, Account);
                break;
            case "Root" :
                stmt = conn.prepareStatement("update user set Root = ? where Account = ?;");
                stmt.setBoolean(1, permission);
                stmt.setInt(2, Account);
                break;
        }

        stmt.executeUpdate();

        conn.close();
    }

    public static void DeleteUser(int Accout) throws SQLException {
        Connection conn = ConnectPool.getConnection();

        PreparedStatement stmt = conn.prepareStatement("delete from user where Account = ?;");
        stmt.setInt(1, Accout);

        stmt.executeUpdate();

        conn.close();
    }

    public static void CreateNewUser() throws SQLException {
        Connection conn = ConnectPool.getConnection();

        User newUser = new User("temp_user");

        PreparedStatement stmt = conn.prepareStatement("insert into user(name,Account,Password,Root,DownloadFile,DeleteFile,UploadFile,RemoteControl) " +
                                                            "values (?,?,?,?,?,?,?,?)");
        stmt.setString(1, newUser.name);
        stmt.setInt(2, newUser.Account);
        stmt.setString(3, newUser.Password);
        stmt.setBoolean(4, newUser.Root);
        stmt.setBoolean(5, newUser.DownloadFile);
        stmt.setBoolean(6, newUser.DeleteFile);
        stmt.setBoolean(7, newUser.UploadFile);
        stmt.setBoolean(8, newUser.RemoteControl);
        stmt.executeUpdate();
        RemoteControllerApplication.userManager.users.add(newUser);

        conn.close();
    }

    public static boolean JustifyUserByName(String userName, String password) throws SQLException {
        Connection conn = ConnectPool.getConnection();
        boolean result = false;

        PreparedStatement stmt = conn.prepareStatement("select exists (SELECT * from user where name=? and Password=?);");
        stmt.setString(1, userName);
        stmt.setString(2, password);

        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            result = rs.getBoolean(1);
        }

        conn.close();
        return result;
    }

    public static boolean JustifyUserByAccount(int Account, String password) throws SQLException {
        Connection conn = ConnectPool.getConnection();
        boolean result = false;

        PreparedStatement stmt = conn.prepareStatement("select exists (SELECT * from user where Account=? and Password=?);");
        stmt.setInt(1, Account);
        stmt.setString(2, password);

        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            result = rs.getBoolean(1);
        }

        conn.close();
        return result;
    }
}
